package com.tg.app.activity.device.settings;

import static com.tg.app.camera.AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_FLIP_MIRROR;
import static com.tg.app.camera.AVIOCTRLDEFs.AVIOCTRL_VIDEOMODE_NORMAL;

import android.os.Bundle;
import android.view.Window;
import android.widget.Switch;

import androidx.annotation.Nullable;
import androidx.core.util.Consumer;

import com.appbase.custom.constant.RotateConstants;
import com.tange.base.toolkit.TGThreadPool;
import com.tange.core.device.facade.ConnectStatus;
import com.tange.core.device.facade.DeviceFacade;
import com.tange.core.device.facade.Status;
import com.tg.app.R;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.camera.Camera;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.android.TGToast;
import com.tg.data.bean.DeviceSettingsInfo;

public class RotateActivity extends DeviceSettingsBaseActivity {
    private static final String TAG = "RotateActivity";
    private DeviceSettingsInfo info;
    private Switch rotate_switch;
    private   int selectMode ;
    private boolean switchState;
    private DeviceFacade deviceFacade;
    private String uuid;
    protected boolean onBackClick(){

        return  true;
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    private final Consumer<ConnectStatus> connectStatusConsumer = connectStatus -> {
        if (connectStatus.getStatus() == Status.SUCCESS){
            TGLog.d(TAG,"连接成功");
        }else if (connectStatus.getStatus() == Status.FAILED){
            TGLog.d(TAG,"连接失败");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        deviceFacade.unObserveConnectStatus(connectStatusConsumer);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_rotate);
        hideActionBar();
        info = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        uuid = getIntent().getStringExtra(Camera.EXT_UUID);
        deviceFacade = new DeviceFacade(uuid);
        deviceFacade.observeConnectStatus(connectStatusConsumer);
        deviceFacade.connect();

        initView();
        backClickEvent();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void initView() {
        modifyToolBar(R.string.settings_picture);
        rotate_switch = findViewById(R.id.roate_switch);
        rotate_switch.setChecked(info.videoMode == RotateConstants.OPPOSITE_DIRECTION);
        rotate_switch.setOnClickListener(view -> {
            if (deviceFacade.connected()){
                switchState = rotate_switch.isChecked();
                if (switchState){
                    setRotate(AVIOCTRL_VIDEOMODE_FLIP_MIRROR);
                }else {
                    setRotate(AVIOCTRL_VIDEOMODE_NORMAL);
                }
            }else{
                TGToast.showToast(R.string.txt_network_anomaly);
                rotate_switch.setChecked(!switchState);
                switchState = !switchState;
            }

        });

    }

    @Override
    protected void onSetupFailed() {
        rotate_switch.setChecked(!switchState);
    }


    public void setRotate(int type) {
        if (deviceFacade.connected()){
            selectMode = type;
            deviceFacade.getInstruct().create(AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SET_VIDEOMODE_REQ).data(AVIOCTRLDEFs.SMsgAVIoctrlSetVideoModeReq.parseContent(0, type, (byte)0)).send(resp -> {
                if (resp.getSuccess()){
                    TGThreadPool.executeOnUiThread(() -> {
                        TGLog.d(TAG,"setRotate success");
                        rotate_switch.setChecked(switchState);
                        mIsSetSuccess = true;
                        info.videoMode = selectMode;
                        sendUpdateSettingBroadcast(info);
                    });
                }else {
                    TGLog.d(TAG,"setRotate failed");
                }
            });
        }else{
            TGToast.showToast(R.string.txt_network_anomaly);
            rotate_switch.setChecked(!switchState);
            switchState = !switchState;
            deviceFacade.connect();
        }

    }
}